<?php
/**
 * Created by PhpStorm.
 * User:雨寒
 * Date: 2018/8/28
 * Time: 11:20
 */

namespace app\www\controller;

use think\Session;
use think\Db;
class Set extends Common
{

    private function get_info(){
        $access_token=Session::get("access_token");
        $data=array(
            "module"=>'Bussiness',
            "interface"=>'1004',
            "version"=>'1.0.0',
            "data"=>array(
                "access_token"=>$access_token
            )
        );
        $res=curl_post(config('self_url'),json_encode($data));

        $result_code=json_decode($res,true)['result_code'];

        $result_info=json_decode($res,true)['result_info'];

        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            $this->error($result_info);
        }

        $result_data=json_decode($res,true)['result_data'];
        return $result_data['info'];
    }

    /**
     *设置首页
     */
    public function index(){
        $info = $this->get_info();
        $info['star_mobile'] = substr_replace($info['mobile'],'****',3,4);
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     *头像设置
     */
    public function setavatar(){
        $access_token=Session::get("access_token");
        $this->assign('access_token',urlencode($access_token));
        if(request()->isGet()){
            $this->assign('info',$this->get_info());
            return $this->fetch();
        }
        if(request()->isAjax()){
            $image = request()->param('image');
            $data=array(
                "module"=>'Member',
                "interface"=>'1008',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "avatar_url"=>$image
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));
            echo $res;
        }
    }

    /**
     *上传头像
     */
    public function upavatar(){
        if(request()->isAjax()){
            header('Content-type:text/html;charset=utf-8');

            $base64_image_content = trim(request()->param('image'));
            //正则匹配出图片的格式
            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {

                $type = $result[2];//图片后缀

                $dateFile = date('Y-m-d', time()) . "/";  //创建目录

                $new_file = './uploads/images/'. $dateFile;

                if (!file_exists($new_file)) {
                    //检查是否有该文件夹，如果没有就创建，并给予最高权限
                    mkdir($new_file, 0700);

                }

                $filename = time() . '_' . uniqid() . ".{$type}"; //文件名

                $new_file = $new_file . $filename;

                //写入操作
                if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))) {

                    $arr = [
                        'result_code'=>0000,
                        'result_info'=>str_replace('./','/',$new_file)
                    ];

                } else {

                    $arr = [
                        'result_code'=>2222,
                        'result_info'=>'上传错误请更换图片重试'
                    ];
                }
                die(json_encode($arr));
            }
        }
    }

    /**
     *设置姓名
     */
    public function setname(){
        $access_token=Session::get("access_token");
        if(request()->isGet()){
            $this->assign('info',$this->get_info());
            return $this->fetch();
        }
        if(request()->isAjax()){
            $content = request()->param('content');

            $data=array(
                "module"=>'Member',
                "interface"=>'1007',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "type"=>1,
                    "content"=>$content
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));
            echo $res;
        }
    }

    /**
     *设置邮箱
     */
    public function setemail(){
        $access_token=Session::get("access_token");
        if(request()->isGet()){
            $this->assign('info',$this->get_info());
            return $this->fetch();
        }
        if(request()->isAjax()){
            $content = request()->param('content');

            $data=array(
                "module"=>'Member',
                "interface"=>'1007',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "type"=>2,
                    "content"=>$content
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));
            echo $res;
        }
    }


    /**
     *修改密码
     */
    public function setpassword(){
        $access_token=Session::get("access_token");
        if(request()->isGet()){
            $this->assign('info',$this->get_info());
            return $this->fetch();
        }
        if(request()->isAjax()){
            $oldpassword = request()->param('oldpassword');
            $newpassword = request()->param('newpassword');
            $confirmpassword = request()->param('confirmpassword');

            $data=array(
                "module"=>'Member',
                "interface"=>'1005',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "oldpassword"=>$oldpassword,
                    "newpassword"=>$newpassword,
                    "confirmpassword"=>$confirmpassword
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));
            echo $res;
        }
    }

    /**
     *修改简介
     */
    public function setabstract(){
        $access_token=Session::get("access_token");
        if(request()->isGet()){
            $this->assign('info',$this->get_info());
            return $this->fetch();
        }
        if(request()->isAjax()){
            $content = request()->param('content');

            $data=array(
                "module"=>'Member',
                "interface"=>'1007',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "type"=>3,
                    "content"=>$content
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));
            echo $res;
        }
    }

    /**
     *修改服务内容
     */
    public function setserver(){
        $access_token=Session::get("access_token");
        if(request()->isGet()){
            $this->assign('info',$this->get_info());
            return $this->fetch();
        }
        if(request()->isAjax()){
            $content = request()->param('content');

            $data=array(
                "module"=>'Member',
                "interface"=>'1007',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "type"=>4,
                    "content"=>$content
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));
            echo $res;
        }
    }
}